﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Ninject;
using SydEnergiPrototype.DataRepositoryServiceRef;
using SydEnergiPrototype.Logic;
using SydEnergiPrototype.Model;

namespace SydEnergiPrototype.DataAccess
{
    public class MemoryDataStore : IMemoryDataStore
    {
        private static string _cabinetId;
        private static SerializableEntry _savedEntry;
        private static Cabinet _savedCabinet;

        private readonly IParserFactory _parserFactory;
        
        [Inject]
        public MemoryDataStore(IParserFactory parserFactory)
        {
            if (parserFactory == null)
                throw new ArgumentNullException("parserFactory");

            _parserFactory = parserFactory;
        }

        public void PersistCabinetId(string id)
        {
            if(string.IsNullOrWhiteSpace(id))
                throw new ArgumentNullException("id");

            _cabinetId = id;
        }

        public void Persist(IEntry entry)
        {
            var parser = _parserFactory.Make(entry.GetType());
            var serializableEntry = parser.ParseToDto<IEntry, SerializableEntry>(entry);
            _savedEntry = Copier.DeepCopy(serializableEntry);
        }

        public void Persist(Cabinet cabinet)
        {
            _savedCabinet = cabinet;
        }

        public string RetrieveCabinetId()
        {
            if(string.IsNullOrWhiteSpace(_cabinetId))
                throw new ArgumentNullException("_cabinetId");
            return _cabinetId;
        }

        public IEntry RetrieveEntry()
        {
            var parser = _parserFactory.Make(_savedEntry.GetType());
            var entry = parser.ParseFromDto<SerializableEntry, Entry>(_savedEntry);
            return entry;
        }

        public Cabinet RetrieveCabinet()
        {
            return _savedCabinet;
        }
    }
}
